home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9983 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  94 lines

  1. Path: wmwap1.math.uni-wuppertal.de!usenet
  2. From: Oliver Heinz <heinz@lfa.uni-wuppertal.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Newbie exception question
  5. Date: Tue, 05 Mar 1996 15:02:25 +0100
  6. Organization: University of Wuppertal, Germany
  7. Message-ID: <313C4971.6956@lfa.uni-wuppertal.de>
  8. NNTP-Posting-Host: welfag.elektro.uni-wuppertal.de
  9. Mime-Version: 1.0
  10. Content-Type: multipart/mixed; boundary="------------1372500F2847"
  11. X-Mailer: Mozilla 2.0 (X11; I; IRIX 5.3 IP20)
  12. CC: heinz@lfa.uni-wuppertal.de
  13.  
  14. This is a multi-part message in MIME format.
  15.  
  16. --------------1372500F2847
  17. Content-Type: text/plain; charset=iso-8859-2
  18. Content-Transfer-Encoding: 7bit
  19.  
  20. Hello out there ...
  21.  
  22. I'm new to exception handling with C++, and so I've tried out a simple
  23. program. It reads one argument from the command line and compares, if
  24. it is a '5'. The program raises the exception, but the catch statement
  25. doesn't work. 
  26.  
  27. Any help is greatly appreciated, thanks in advance
  28.  
  29. Oliver
  30.  
  31.  
  32.  
  33. The code:
  34.  
  35. #include <stdlib.h>
  36.  
  37. #include <iostream.h>
  38.  
  39. int 
  40. main(int argc, char *const *argv)
  41. {
  42.   long int  _myint;
  43.  
  44.   try {
  45.     cout << "The first command line option was: " << argv[1] << endl;
  46.     _myint=strtol(argv[1], NULL, 10);   
  47.     if (_myint==5) {
  48.       cout << "Succesfull comparison, raising exception." << endl;
  49.       throw (_myint);
  50.     }
  51.   }
  52.   catch (long int i) {
  53.     cout << "An exception occurred: " << i << endl;
  54.   }
  55.  
  56.   cout << "Normal program exit ..." << endl;
  57.   return 0;
  58. }
  59.  
  60.  
  61.  
  62. Compiled with gcc version 2.7.2:
  63.  
  64. gcc -o tmp -fhandle-exceptions tmp.C -lstdc++
  65.  
  66. (No warnings, no errors)
  67.  
  68.  
  69.  
  70. The output:
  71.  
  72. > tmp 5
  73. The first command line option was: 5
  74. Succesfull comparison, raising exception.
  75. Normal program exit ...
  76.  
  77. (Oops, where is the cout from the catch statement ?)
  78.  
  79. --------------1372500F2847
  80. Content-Type: text/plain; charset=iso-8859-2
  81. Content-Transfer-Encoding: 7bit
  82. Content-Disposition: inline; filename="absender"
  83.  
  84. ============================================================================
  85. Oliver Heinz                            |    E-mail: 
  86. University of Wuppertal                 |    heinz@lfa.uni-wuppertal.de
  87. Department of Electrical Engineering    |
  88. Fuhlrottstr. 10                         |
  89. D-42110 Wuppertal, GERMANY              |
  90. ============================================================================
  91.  
  92. --------------1372500F2847--
  93.  
  94.